package com.example.administrator.idocment.docment.tool;

import android.media.MediaMetadataRetriever;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.administrator.idocment.BaseTypeFind;
import com.example.administrator.idocment.tooltime.TimerFactory;

import java.io.File;

/**
 * Created by Administrator on 2017/7/17 0017.
 */
public class MoreInfo implements IMore.FileInfo,IMore.FolderInfo,MultiItemEntity
{

    public static final  int fileType=1;
    public static final  int dirType=2;

    Boolean selectState=false;
    Boolean upState=false;
    Boolean proState=false;

    private File file;
    String fileName;
    public MoreInfo(File file)
    {
        this.file=file;
        fileName=file.getName();
    }



    @Override
    public Boolean getUpState() {
        return UpFlag.upKey.get(file.getAbsolutePath())!=null;
    }

    @Override
    public void setUpState(Boolean upState) {
        if(upState)
        {
            UpFlag.upKey.put(file.getAbsolutePath(),true);
        }

        this.upState=upState;

    }

    @Override
    public Boolean getSelectState() {
        return selectState;
    }

    @Override
    public void setSelectState(Boolean selectState) {
       this.selectState=selectState;
    }

    @Override
    public Boolean getProState() {
        return proState;
    }

    @Override
    public void setProState(Boolean proState) {
       this.proState=proState;
    }


    @Override
    public String getName() {
        return fileName;
    }


    @Override
    public String getTime() {
        return TimerFactory.create(TimerFactory.YearFormat).paseTimer(file.lastModified());
    }


    @Override
    public String getSize() {
        return BaseTypeFind.getFileSzie(file.length());
    }

    @Override
    public FileEnum getType() {
        return CheckEnum.getType(getMimeType(file.getPath()),file);
    }

    @Override
    public String getPath() {
        return file.getPath();
    }

    @Override
    public String getParent() {
        return file.getParent();
    }


    @Override
    public void setFolderName(String fileName) {
        this.fileName=fileName;
    }

    @Override
    public String getFolderName() {
        return fileName;
    }





    public static String getExtension(final File file) {
        String suffix = "";
        String name = file.getName();
        final int idx = name.lastIndexOf(".");
        if (idx > 0) {
            suffix = name.substring(idx);
        }
        return suffix;
    }



    public static String getMimeType(String filePath) {

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        String mime = "?";
        if (filePath != null) {
            try {
                mmr.setDataSource(filePath);
                mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
            } catch (IllegalStateException e) {
                return mime;
            } catch (IllegalArgumentException e) {
                return mime;
            } catch (RuntimeException e) {
                return mime;
            }
        }
        android.util.Log.e("mime",mime);
        return mime;
    }




    @Override
    public int getItemType() {
        if(file.isDirectory()) {

            return dirType;
        }
        else
            return fileType;
    }


}
